﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;

public partial class SYS_POPEDOM_Dept_Role_Manage : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BuildTreeRoot();
            AddPopWin();
        }
    }
    

    //添加弹出窗口
    private void AddPopWin()
    {
        string url = G_Url_allstring + "/SYS_POPEDOM/Dept_Role_Edit.aspx";
        extBtn_Add.Listeners.Click.Handler = "popforwin(GridPanel1,'" + url + "',#{TreePanel1}.getSelectionModel().getSelectedNode().text,600,400,'ObjId','Add',0,'DeptId='+#{TreePanel1}.getSelectionModel().getSelectedNode().id ,1) ;";
    }

    //构建部门树根节点
    private void BuildTreeRoot()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_no").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            DataRow rootRow = tmpDt.Rows[0];
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = rootRow["dept_name"].ToString();
            root.NodeID = rootRow["dept_id"].ToString();
            root.Expanded = true;
            root.IconCls = "no-icon";
            root.Leaf = rootRow["leaf"].ToString() == "0";
            this.TreePanel1.Root.Add(root);
        }
    }

    //加载下属节点
    [DirectMethod]
    public string NodeLoad(string nodeID)
    {
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and ltrim(rtrim(dept_id)) like '" + nodeID.Trim() + "__' ", "dept_no").Tables[0];
            foreach (DataRow nodeRow in TreeData.Rows)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = nodeRow["dept_name"].ToString();
                asyncNode.NodeID = nodeRow["dept_id"].ToString();
                asyncNode.IconCls = "no-icon";
                asyncNode.Leaf = nodeRow["leaf"].ToString() == "0";
                nodes.Add(asyncNode);
            }
        }
        BindGrid(nodeID);
        return nodes.ToJson();
    }

    //单击树节点


    [DirectMethod]
    public void NodeClick(string NodeId)
    {
        exthid_deptId.Value = NodeId.Trim();
        BindGrid(NodeId);
    }

    //加载列表
    private void BindGrid(string strDEPT_ID)
    {
        DataTable GridData = CommonLib.BLL.SYS_DEPT_ROLES.GetList(" ltrim(rtrim(DEPT_ID)) = '" + strDEPT_ID.Trim() + "' and creator IN (SELECT su.[USER_ID] FROM SYS_USER su WHERE Charindex(RTRIM('" + BS_LoginDeptId + "'),su.DEPT_ID,0) > 0 ) ", "ROLE_ID").Tables[0];
        this.Store1.DataSource = GridData;
        this.Store1.DataBind();
        //SelectFristData(GridPanel1, GridData, "");
    }

    //删除角色
    protected void DelDept(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }

        X.Msg.Confirm("系统提示", "是否删除选中行?", new  MessageBoxButtonsConfig
        {
            Yes = new MessageBoxButtonConfig
            {
                Handler = "Ext.net.DirectMethods.DoDelMenu();refreshTree();",
                Text = "是"
            },
            No = new MessageBoxButtonConfig
            {
                Text = "否"
            }
        }).Show();

    }

    //删除菜单
    [DirectMethod]
    public void DoDelMenu()
    {
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        foreach (SelectedRow row in sm.SelectedRows)
        {
            CommonLib.BLL.SYS_DEPT_ROLES.Delete(int.Parse(row.RecordID));
        }

    }

}
